var diff = require('arr-diff')


/**
 * 只能有2个参数
 * @param  {[type]} arr1 [description]
 * @param  {[type]} arr2 [description]
 * @return {[type]}      [description]
 */
function arrayDiff(arr1, arr2) {
	if(!Array.isArray(arr1) ) {
		throw new TypeError('the first argument must be an array')
	}

	if(!Array.isArray(arr2) ) {
		return arr1.slice()
	}

	var diff = []

	var useIndexs = []
	var one 
	
	// 找出不在arr2中，但是在arr1中的元素
	for (var i = 0; i < arr1.length; i++) {
		one = arr1[i]
		if(!arr2.includes(one)) {
			diff.push(one)
			useIndexs.push(i)
		}
	}

	return diff
}

/**
 * 支持不定参数
 * 
 * @param  {[type]} arr [description]
 * @return {[type]}     [description]
 */
function arrayDiffV2(arr) {
	if(!Array.isArray(arr) ) {
		throw new TypeError('the first argument must be an array')
	}

	var len = arguments.length;
	for(var i = 1; i < len; i++) {
		arr = diffArray(arr, arguments[i])
	}
	return arr

	;function diffArray(one, two) {
		if(!Array.isArray(two) ) {
			return one.slice()
		}
		var ele, diff = []
		// 找出不在two中，但是在two中的元素
		for (var i = 0; i < one.length; i++) {
			ele = one[i]
			if( !two.includes(ele) ) {
				diff.push(ele)
			}
		}
		return diff
	}
}

/**
 * filter + set 进行筛选
 * @param  {[type]}    arr    [description]
 * @param  {...[type]} values [description]
 * @return {[type]}           [description]
 */
function arrayDiffV3(arr, ...values) {
	// 先变为Set 进行数据去重
	var rest = new Set([].concat(...values))

	// 再通过数组的过滤函数进行筛选不在set结构中的数据
	return arr.filter(item => !rest.has(item))
}


var a = ['a', 'b', 'c', 'd', NaN, 2];
var b = ['b', 'c'];

console.log(diff(a, b))
//=> ['a', 'd']
//
console.log('-----------------------\n')

console.log(arrayDiff(a, b))
console.log(arrayDiffV2(a, b))
console.log(arrayDiffV3(a, b))
console.log('-----------------------\n')

console.log(diff(a, b, [NaN, 2, 'a']))
console.log(arrayDiffV2(a, b, [NaN, 2, 'a']))
console.log(arrayDiffV3(a, b, [NaN, 2, 'a']))
